﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CommonLibraries.DeviceLibrary;
using PolePositionMobileUtilityEntity;
using PolePositionMobileUtilityGeneral;
using PolePositionMobileUtilityEntity.Entities;
using PolePositionMobileUtilityEntity.Logic;

namespace PolePositionMobileUtilityApplication
{
    public partial class frmSetting : frmBase
    {
        private List<Int32> m_ValuesInspector;
        private List<Int32> m_ValuesCompany;
        private ESetting m_Settings;
        private EProject[] m_Projects;
        private EContact[] m_Contacts;
        private EInspector[] m_Inspectors;
        private List<Int32> m_ValuesProject;
        private List<Int32> m_ValuesContact;
        private List<Int32> m_ValuesVendor;
        private List<Int32> m_ReceptionModes;

        public frmSetting()
        {
            InitializeComponent();
            
        }

        private void LoadCombos()
        {
            ECompany co = new ECompany();
            ECompany[] coArray = co.GetAll();
            //[Company]
            m_ValuesCompany = new List<int>();
            m_ValuesCompany = MobileUIHelper.FillDropdown(ddlCompany, coArray, "Name", "Id", "[Company]");

            //[Inspector]

            m_Inspectors = new EInspector().GetAll();
            m_ValuesInspector = MobileUIHelper.FillDropdown(ddlInspector, m_Inspectors, "Name", "Id", "[Inspector]");

            //[Vendor]
            EVendor[] vArray = new EVendor().GetAll();
            m_ValuesVendor = new List<int>();
            m_ValuesVendor = MobileUIHelper.FillDropdown(ddlVendor, vArray, "Name", "Id", "[Vendor]");

            //ReceptionMode
            m_ReceptionModes = new List<int>{(int)ReceptionMode.AMode, (int)ReceptionMode.BMode};
            ddlMode.Items.Clear();
            ddlMode.Items.Add("Mode A");
            ddlMode.Items.Add("Mode B");

            m_Contacts = new EContact().GetAll();
            m_Projects = new EProject().GetAll();
        }

        private void LoadData()
        {
            m_Settings = LSetting.GetAll();
            MobileUIHelper.SetSelectedValue(m_ValuesCompany, m_Settings.Company.Id, ddlCompany);
            MobileUIHelper.SetSelectedValue(m_ValuesInspector, m_Settings.Inspector.Id, ddlInspector);
            MobileUIHelper.SetSelectedValue(m_ValuesVendor, m_Settings.Vendor.Id, ddlVendor);
            ddlMode.SelectedIndex = m_Settings.ReceptionMode;
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {
            #region Config form
            lblTitle.Text = "PPI | Settings";
            SetLButton("Save");
            SetRButton("");
            #endregion
            LoadCombos();
            LoadData();
        }

        protected override void lblLButton_Click(object sender, EventArgs e)
        {
            m_Settings.Company.Id = MobileUIHelper.GetSelectedValue(m_ValuesCompany, ddlCompany);
            m_Settings.Company.Name = ddlCompany.SelectedText;

            m_Settings.Inspector.Id = MobileUIHelper.GetSelectedValue(m_ValuesInspector, ddlInspector);
            m_Settings.Inspector.Name = ddlInspector.SelectedText;

            m_Settings.Vendor.Id = MobileUIHelper.GetSelectedValue(m_ValuesVendor, ddlVendor);
            m_Settings.Vendor.Name = ddlVendor.SelectedText;

            m_Settings.Contact.Id = MobileUIHelper.GetSelectedValue(m_ValuesContact, ddlContact);
            m_Settings.Contact.Name = ddlContact.SelectedText;

            m_Settings.Project.Id = MobileUIHelper.GetSelectedValue(m_ValuesProject, ddlProject);
            m_Settings.Project.Name = ddlProject.SelectedText;

            m_Settings.ReceptionMode = ddlMode.SelectedIndex;

            if(ValidateResults())
            {
                LSetting.Insert(m_Settings);
                m_GPS.ChangeToMode((ReceptionMode)m_Settings.ReceptionMode);
                Close();
            }
        }

        private bool ValidateResults()
        {
            bool _isValid = true;
            StringBuilder _Message = new StringBuilder();
            if(m_Settings.Company.Id == Int32.MinValue)
            {
                _isValid = false;
                _Message.AppendLine("Select a Company");
            }
            if(m_Settings.Inspector.Id == Int32.MinValue)
            {
                _isValid = false;
                _Message.AppendLine("Select a Inspector");
            }
            if (m_Settings.Vendor.Id == Int32.MinValue)
            {
                _isValid = false;
                _Message.AppendLine("Select a Vendor");
            }
            if (m_Settings.Contact.Id == Int32.MinValue && ddlContact.Enabled)
            {
                _isValid = false;
                _Message.AppendLine("Select a Contact");
            }
            if (m_Settings.Project.Id == Int32.MinValue && ddlProject.Enabled)
            {
                _isValid = false;
                _Message.AppendLine("Select a Project");
            }

            if (!_isValid)
                MessageBox.Show(_Message.ToString());
            return _isValid;
        }

        protected override void lblClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 selectedVendorId = MobileUIHelper.GetSelectedValue(m_ValuesVendor, ddlVendor);
            if(selectedVendorId > 0)
            {
                ddlProject.Enabled = true;
                ddlContact.Enabled = true;

                List<EContact> listContacts = new List<EContact>();
                foreach (EContact contact in m_Contacts)
                {
                    if(contact.Vendor.Id == selectedVendorId)
                        listContacts.Add(contact);
                }
                m_ValuesContact = MobileUIHelper.FillDropdown(ddlContact, listContacts.ToArray(), "Name", "Id", "[Contact]");
                MobileUIHelper.SetSelectedValue(m_ValuesContact, m_Settings.Contact.Id, ddlContact);

                List<EProject> listProjects = new List<EProject>();
                foreach (EProject project in m_Projects)
                {
                    if(project.Vendor.Id == selectedVendorId)
                        listProjects.Add(project);
                }
                m_ValuesProject = MobileUIHelper.FillDropdown(ddlProject, listProjects.ToArray(), "Name", "Id", "[Project]");
                MobileUIHelper.SetSelectedValue(m_ValuesProject, m_Settings.Project.Id, ddlProject);
            }else
            {
                ddlProject.Enabled = false;
                ddlContact.Enabled = false;
            }
        }

        private void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 selectedCompanyId = MobileUIHelper.GetSelectedValue(m_ValuesCompany, ddlCompany);

            if (selectedCompanyId > 0)
            {
                List<EInspector> lstInspector = new List<EInspector>();
                
                foreach (EInspector inspector in m_Inspectors)
                {
                    if (inspector.Company.Id == selectedCompanyId)
                        lstInspector.Add(inspector);
                }
                
                m_ValuesInspector = MobileUIHelper.FillDropdown(ddlInspector, lstInspector.ToArray(), "Name", "Id", "[Inspector]");
            }
        }
    }
}